package com.infinit.easyreader.adpter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.infinit.easyreader.R;
import com.infinit.easyreader.entity.RankContent;

import java.util.ArrayList;
import java.util.List;

/**
 * 类：com.infinit.easyreader.adpter.RankRecyclerAdapter V[1.0.0]
 * 作用：用于显示排行信息的RecyclerView的Adapter
 * Created by rzy on 2016/7/2.
 */
public class RankRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    public static final int LOAD_STATUS_LOADING = 1;
    public static final int LOAD_STATUS_TO_LOAD = 0;
    private static final int TYPE_ITEM_NORMAL = 1;
    private static final int TYPE_ITEM_FOOTER = 2;
    private List<RankContent> data;
    private int load_status = 0;
    private OnItemClickListener onItemClickListener;
    private int position;


    public RankRecylerAdapter(List<RankContent> data) {
        this.data = data;
        if (data == null) {
            this.data = new ArrayList<>();
        }
    }

    public RankRecylerAdapter(List<RankContent> data, OnItemClickListener onItemClickListener) {
        this.data = data;
        this.onItemClickListener = onItemClickListener;
    }

    public int getPosition() {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setLoad_status(int load_status) {
        this.load_status = load_status;
        notifyDataSetChanged();
    }

    public void setData(List<RankContent> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_ITEM_NORMAL:
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rank_content, parent, false);
                if (onItemClickListener != null) {
                    view.setOnClickListener(this);
                }
                view.setTag(position + "");
                return new ViewHolder(view);
            case TYPE_ITEM_FOOTER:
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_footer, parent, false);
                return new FooterViewHolder(v);
        }
        return null;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof com.infinit.easyreader.adpter.RankRecylerAdapter.ViewHolder) {
            ViewHolder mHolder = (ViewHolder) holder;
            RankContent rankContent = data.get(position);
            mHolder.tvClickCount.setText(rankContent.getClickCount() + "");
            mHolder.tvAuthor.setText(genShortString(rankContent.getAuthorName()));
            mHolder.tvBookName.setText(genShortString(rankContent.getContentName()));
            mHolder.tvRankCount.setText(position + 1 + "");
            mHolder.tvRankCount.setTextColor(Color.BLACK);
            if (position <= 2) {
                mHolder.tvRankCount.setTextColor(Color.RED);
            }
        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder mHolder = (FooterViewHolder) holder;
            switch (load_status) {
                case LOAD_STATUS_LOADING:
                    mHolder.tvInfo.setText("正在加载中...");
                    break;
                case LOAD_STATUS_TO_LOAD:
                    mHolder.tvInfo.setText("上拉加载数据");
                    break;
            }

        }
    }


    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    /**
     * 缩略字方法
     */

    private String genShortString(String longString) {

        if (longString.length() <= 8) {
            return longString.trim();
        } else {
            return longString.trim().substring(0, 6) + "...";
        }


    }

    @Override
    public int getItemViewType(int position) {
        this.position = position;
        if (position == getItemCount() - 1) {
            return TYPE_ITEM_FOOTER;
        }
        return TYPE_ITEM_NORMAL;
    }

    public void addMoreData(List<RankContent> contents) {
        data.addAll(contents);
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onItemClick(Integer.parseInt((String) v.getTag()), v);

    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvRankCount;
        TextView tvBookName;
        TextView tvAuthor;
        TextView tvClickCount;

        public ViewHolder(View itemView) {
            super(itemView);
            tvRankCount = (TextView) itemView.findViewById(R.id.tvRankCount);
            tvBookName = (TextView) itemView.findViewById(R.id.tvRankBookName);
            tvAuthor = (TextView) itemView.findViewById(R.id.tvRankAuthor);
            tvClickCount = (TextView) itemView.findViewById(R.id.tvClickCount);
        }
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        TextView tvInfo;

        public FooterViewHolder(View itemView) {
            super(itemView);
            tvInfo = (TextView) itemView.findViewById(R.id.tvLoadFooter);
        }
    }
}
